var datalist = []
var newsBookIdList = []
$().ready(function () {
    validateRule();
    getCertificateList()
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});


layui.use('transfer', function () {
    var transfer = layui.transfer;
    // 数据
    // 渲染
    transfer.render({
        elem: '#transferdemovalue',
        parseData: function (res) {
            return {
                "value": res.id, // 数据值
                "title": res.profileName, // 数据标题
                "newsList":res
            }
        },
        data: datalist,
        title: ['总证书列表', '选择证书列表'],
        height: 400,// 定义高度
        width: 400, // 定义高度
        onchange: function (obj, index) {

            var arr = ['左边', '右边'];
            if (arr[index] === '左边') {
                // 确保newsBookIdList是一个数组，然后添加数据
                if (!Array.isArray(newsBookIdList)) {
                    newsBookIdList = [];
                }
                // 添加数据到数组，这里直接添加obj，因为它是从transfer中直接获取的，无需再构造
                obj.forEach((item, index) => {
                    newsBookIdList.push(item);
                })

            } else if (arr[index] === '右边') {
                newsBookIdList = newsBookIdList.filter(eitem =>
                    !obj.some(uitem => eitem.value === uitem.value)
                );
                // 移除数据，注意这里使用value进行过滤，确保与添加逻辑一致
                // newsBookIdList.splice(index,1)
            }
            console.log(newsBookIdList)
        }
        //批量办法定事件

    });
});

function getCertificateList() {
    $.ajax({
        cache: true,
        type: "GET",
        contentType: 'application/json',
        url: "/jzweb/eventResourceTemplate/getCertificateList",
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                datalist = data.data
            }

        }
    });
}

function save() {
    if(newsBookIdList.length>5){
        layer.msg('证书不能超过5条！')
        return false;
    }
    $.ajax({
        cache: true,
        type: "POST",
        contentType: 'application/json',
        url: "/jzweb/eventResourceTemplate/save",
        data: JSON.stringify({
            name: $('#name').val(),
            type: $('#type').val(),
            newsIdList: newsBookIdList,
            duration: String($('#duration').val()),
            totalScore: Number($('#totalScore').val()),
        }),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入姓名"
            }
        }
    })
}